import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_yn_goods/base/widget/base_list_more_get_builder.dart';
import 'package:flutter_yn_goods/config/app_color.dart';
import 'package:flutter_yn_goods/config/app_routes.dart';
import 'package:flutter_yn_goods/config/app_translations.dart';
import 'package:flutter_yn_goods/pages/mine_collect/model/model_collect_list.dart';
import 'package:flutter_yn_goods/utils/utils_other.dart';
import 'package:flutter_yn_goods/widgets/appbar/common_app_bar.dart';
import 'package:flutter_yn_goods/widgets/image/image_view.dart';
import 'package:get/get.dart';

import 'mine_collect_logic.dart';

class MineCollectPage extends StatelessWidget {
  final logic = Get.put(MineCollectLogic(), tag: MineCollectLogic.tag);

  MineCollectPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: CommonAppBar(
        title: LocaleKeys.my_collections.tr,
        actions: [
          GestureDetector(
            behavior: HitTestBehavior.opaque,
            onTap: () {
              logic.toggleManageMode();
            },
            child: Container(
              padding: EdgeInsets.symmetric(horizontal: 8.w),
              alignment: Alignment.center,
              child: Obx(() => Text(
                    logic.isManageMode.value
                        ? LocaleKeys.cancel.tr
                        : LocaleKeys.manage.tr,
                    style: TextStyle(
                      color: AppColor.color282828,
                      fontSize: 14.sp,
                    ),
                  )),
            ),
          ),
        ],
      ),
      body: Column(
        children: [
          Expanded(
            child: _buildCollectionList(),
          ),
          // 底部管理操作栏，在管理模式下显示
          Obx(() {
            return logic.isManageMode.value ? _buildBottomBar() : SizedBox();
          }),
        ],
      ),
    );
  }

  Widget _buildCollectionList() {
    return BaseListMoreGetBuilder<MineCollectLogic>(
      tag: MineCollectLogic.tag,
      enablePullDown: true,
      enablePullUp: true,
      initialRefresh: true,
      builder: (controller) {
        return ListView.builder(
          padding: EdgeInsets.all(16.w),
          itemCount: controller.list.length,
          itemBuilder: (context, index) {
            ModelCollectListList item =
                controller.list[index] as ModelCollectListList;
            return _buildCollectItem(item, index);
          },
        );
      },
    );
  }

  Widget _buildCollectItem(ModelCollectListList item, int index) {
    return GestureDetector(
      onTap: () {
        if (logic.isManageMode.value) {
          // 管理模式下，点击选择商品
          logic.toggleItemSelection(index);
        } else {
          // 非管理模式下，点击进入商品详情
          Get.toNamed(AppRoutes.goodsInfoPage,
              arguments: {'id': item.productId});
        }
      },
      child: Container(
        margin: EdgeInsets.only(bottom: 12.w),
        padding: EdgeInsets.all(12.w),
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(8.w),
          boxShadow: [
            BoxShadow(
              color: Colors.black.withOpacity(0.05),
              blurRadius: 4,
              offset: Offset(0, 2),
            ),
          ],
        ),
        child: Row(
          children: [
            // 管理模式下显示选择框
            Obx(() {
              return logic.isManageMode.value
                  ? Container(
                      margin: EdgeInsets.only(right: 10.w),
                      child: Checkbox(
                        value: item.isSelected,
                        onChanged: (value) {
                          logic.toggleItemSelection(index);
                        },
                        activeColor: AppColor.primary,
                      ))
                  : SizedBox();
            }),
            // 商品图片
            ClipRRect(
              borderRadius: BorderRadius.circular(8.w),
              child: ImageView(
                imageUrl: item.image,
                width: 80.w,
                height: 80.w,
                fit: BoxFit.cover,
              ),
            ),
            SizedBox(width: 12.w),
            // 商品信息
            Expanded(
              child: Container(
                height: 80.w,
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    // 商品名称
                    Text(
                      item.storeName ?? '',
                      maxLines: 2,
                      overflow: TextOverflow.ellipsis,
                      style: TextStyle(
                        color: AppColor.color282828,
                        fontSize: 14.sp,
                        fontWeight: FontWeight.w500,
                      ),
                    ),
                    // 商品价格
                    Text(
                      // '¥${item.price ?? ''}',
                      UtilsOther.getGoodsPrice(item.price),
                      style: TextStyle(
                        color: AppColor.primary,
                        fontSize: 16.sp,
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                  ],
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildBottomBar() {
    return Container(
      height: 56.h,
      padding: EdgeInsets.symmetric(horizontal: 16.w),
      decoration: BoxDecoration(
        color: Colors.white,
        boxShadow: [
          BoxShadow(
            color: Colors.black.withOpacity(0.05),
            blurRadius: 4,
            offset: Offset(0, -1),
          ),
        ],
      ),
      child: Row(
        children: [
          // 全选
          GestureDetector(
            onTap: () {
              logic.toggleSelectAll();
            },
            child: Row(
              children: [
                Obx(() {
                  return Checkbox(
                    value: logic.isAllSelected.value,
                    onChanged: (value) {
                      logic.toggleSelectAll();
                    },
                    activeColor: AppColor.primary,
                  );
                }),
                Text(
                  LocaleKeys.select_all.tr,
                  style: TextStyle(
                    color: AppColor.color282828,
                    fontSize: 14.sp,
                  ),
                ),
              ],
            ),
          ),
          SizedBox(width: 8.w),
          // 已选数量
          Obx(() {
            return Text(
              '${LocaleKeys.selected.tr}: ${logic.selectedCount.value}',
              style: TextStyle(
                color: AppColor.color666666,
                fontSize: 14.sp,
              ),
            );
          }),
          Spacer(),
          // 取消收藏按钮
          GestureDetector(
            onTap: () {
              logic.batchCancelCollect();
            },
            child: Container(
              padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 8.h),
              decoration: BoxDecoration(
                color: AppColor.primary,
                borderRadius: BorderRadius.circular(20.w),
              ),
              child: Text(
                LocaleKeys.remove_collection.tr,
                style: TextStyle(
                  color: Colors.white,
                  fontSize: 14.sp,
                  fontWeight: FontWeight.w500,
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }
}
